package action.contract;

import java.util.List;

import service.contract.ContractService;
import service.contract.ShowContractService;
import util.Sessions;
import util.Time;
import model.company.Companybasic;
import model.company.Companypledger;
import model.contract.Contractmortgage;
import model.contract.Contractpledge;
import model.credit.Applycustomrelation;
import model.personal.Personalbasic;
import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;

public class ContractRelationNewAction extends BaseAction{
	private String contractY;
	private String contractnumber;
	private String customcode;
	private String certificatenumber;
	private Contractmortgage contractmortgage;
	private Contractpledge contractpledge;
	private ContractService contractService;
	private ShowContractService showContractService;
	private Integer relationid;

	public String SaveinMortgage() throws Exceptions{
		Integer creditid=(Integer)Sessions.get("creditid");
		List<Contractmortgage> idList=showContractService.get_htMortgage();
		String contractloanid=showContractService.get_ht("applyid",creditid).getContractId();
		String mortgageid="";
		Integer customid=contractService.getapplyrelation("id", relationid).getClientid();
		if(contractmortgage.getMortgagetype().equals("1")){
			mortgageid=contractY+"年个抵字第"+contractnumber+"号";
			Personalbasic personal=contractService.get_personalbasic(customid);
			contractmortgage.setCustommortgageid(personal.getId());
			contractmortgage.setCustommortgagetype("1");
		}else if(contractmortgage.getMortgagetype().equals("2")){
			mortgageid=contractY+"年企抵字第"+contractnumber+"号";
			Companybasic company=contractService.get_companybasic(customid);
			contractmortgage.setCustommortgageid(company.getId());
			contractmortgage.setCustommortgagetype("2");
		}
		Applycustomrelation applyrelation=contractService.getapplyrelation("id", (Integer)Sessions.get("relationid"));
		Companypledger things=contractService.get_customthings(applyrelation.getThingsid());
		contractmortgage.setThingsid(things.getId());
		if(idList != null){
		for(Contractmortgage m:idList){
			if(m.getCustommortgageid().equals(mortgageid)){
				MessageJump("重复的合同编号","BACK");
				return "message";
			}
		}
		}
		contractmortgage.setMortgageid(mortgageid);
		contractmortgage.setContractloanid(contractloanid);
		contractmortgage.setCreatetime(Time.CurrentTimestamp());
		contractmortgage.setClerkid(Global.userid().intValue());
		contractmortgage.setState("10");
		if(contractmortgage.getContractensuredate().after(contractmortgage.getCreatetime())){
			MessageJump("合同签订日期填写有误！","BACK");
			return "message";
		}
		contractService.save_in_contractmortgage(contractmortgage);
		//修改relation的状态。
		applyrelation.setState("0");
		contractService.save_in_applycustomrelation(applyrelation);
		if(contractService.get_countrelation(creditid,1)==0){
		if(contractService.get_countrelation(creditid, 2)!=0){
				MessageJump("抵押合同已保存，接下来将生成质押合同","contractRelation!getPledgeList");
		}else if(contractService.get_countrelation(creditid, 3)!=0){
				MessageJump("借款合同已保存，接下来将生成保证合同","contractEnsureListAction!getList");
		 }else{
				MessageJump("操作已完成","showContract!showcheck");
		 }
		}else{
				MessageJump("还有未生成的抵押合同","contractRelation!getMortgageList");
		}
		return "message";
	}
	public String SaveinPledge() throws Exceptions{
		Integer creditid=(Integer)Sessions.get("creditid");
		List<Contractpledge> idList=showContractService.get_htPledge();
		String contractloanid=showContractService.get_ht("applyid",creditid).getContractId();
		String pledgeid="";
		Integer customid=contractService.getapplyrelation("id", relationid).getClientid();
		if(contractpledge.getPledgetype().equals("1")){
			pledgeid=contractY+"年个质字第"+contractnumber+"号";
			Personalbasic personal=contractService.get_personalbasic(customid);
			contractpledge.setCustompledgeid(personal.getId());
			contractpledge.setCustompledgetype("1");
		}else if(contractpledge.getPledgetype().equals("2")){
			pledgeid=contractY+"年企质字第"+contractnumber+"号";
			Companybasic company=contractService.get_companybasic(customid);
			contractpledge.setCustompledgeid(company.getId());
			contractpledge.setCustompledgetype("2");
		}
		Applycustomrelation applyrelation=contractService.getapplyrelation("id", (Integer)Sessions.get("relationid"));
		Companypledger things=contractService.get_customthings(applyrelation.getThingsid());
		contractpledge.setThingsid(things.getId());
		contractpledge.setContractloanid(contractloanid);
		if(idList != null){
		for(Contractpledge p:idList){
			if(p.getCustompledgeid().equals(pledgeid)){
				MessageJump("重复的合同编号","BACK");
				return "message";
			}
		}
		}
		contractpledge.setPledgeid(pledgeid);
		contractpledge.setCreatetime(Time.CurrentTimestamp());
		contractpledge.setClerkid(Global.userid().intValue());
		contractpledge.setState("10");
		if(contractpledge.getContractensuredate().after(contractpledge.getCreatetime())){
			MessageJump("合同签订日期填写有误！","BACK");
			return "message";
		}
		contractService.save_in_contractpledge(contractpledge);
		applyrelation.setState("0");
		contractService.save_in_applycustomrelation(applyrelation);
		if(contractService.get_countrelation(creditid,2)==0){
			if(contractService.get_countrelation(creditid, 3)!=0){
						MessageJump("质押合同已保存，接下来将生成保证合同","contractEnsureListAction!getList");
					}else{
						MessageJump("操作已完成","showContract!showcheck");
		     }
		}else{
			MessageJump("还有未生成的质押合同","contractRelation!getPledgeList");
		}
		
	    return "message";
	}
	//---------------GET/SET--------------//
	public Contractmortgage getContractmortgage() {
		return contractmortgage;
	}

	public void setContractmortgage(Contractmortgage contractmortgage) {
		this.contractmortgage = contractmortgage;
	}

	public Contractpledge getContractpledge() {
		return contractpledge;
	}

	public void setContractpledge(Contractpledge contractpledge) {
		this.contractpledge = contractpledge;
	}

	public String getContractY() {
		return contractY;
	}

	public void setContractY(String contractY) {
		this.contractY = contractY;
	}

	public String getContractnumber() {
		return contractnumber;
	}

	public void setContractnumber(String contractnumber) {
		this.contractnumber = contractnumber;
	}

	public String getCustomcode() {
		return customcode;
	}

	public void setCustomcode(String customcode) {
		this.customcode = customcode;
	}

	public String getCertificatenumber() {
		return certificatenumber;
	}

	public void setCertificatenumber(String certificatenumber) {
		this.certificatenumber = certificatenumber;
	}
	public ContractService getContractService() {
		return contractService;
	}
	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}
	public ShowContractService getShowContractService() {
		return showContractService;
	}
	public void setShowContractService(ShowContractService showContractService) {
		this.showContractService = showContractService;
	}
	public Integer getRelationid() {
		return relationid;
	}
	public void setRelationid(Integer relationid) {
		this.relationid = relationid;
	}

}
